#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>

int num;
pthread_mutex_t mutex;

void * add(void *arg) {
    pthread_mutex_lock(&mutex);
    for (int i = 0; i < 10000; i++)
        num++;
    pthread_mutex_unlock(&mutex);
    return NULL;
}

void * sub(void *arg) {
    pthread_mutex_lock(&mutex);
    for (int i = 0; i < 10000; i++)
        num--;
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main() {
    pthread_t pid[100];
    pthread_mutex_init(&mutex, NULL);
    for (int i = 0; i < 100; i++) {
        if (i % 2 == 0) {
            pthread_create(&pid[i], NULL, add, NULL);
        } else {
            pthread_create(&pid[i], NULL, sub, NULL);
        }
    }
    for (int i = 0; i < 100; i++)
        pthread_join(pid[i], NULL);
    printf("num = %d", num);
    return 0;
}